class Solution:
    def countDigitOne(self, n: int) -> int:
        if n <=0:
            return 0
        old = n
        oneSum = 0
        bitSum = 1
        while n:
            rest = n % 10
            if rest > 1:
                oneSum = oneSum + bitSum
            elif rest == 1:
                oneSum = oneSum + old % bitSum + 1
            n = n // 10
            oneSum = oneSum + bitSum * n
            bitSum = bitSum * 10
        return oneSum


if __name__ == '__main__':
    so = Solution()
    s = 30
    r = so.countDigitOne(s)
    print(r)
